package cn.tedu.weibo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;
//上传图片
@RestController
public class UploadController {
    @RequestMapping("/upload")
    public String upload(MultipartFile pic) throws IOException {
        //pic 上传文件参数，和前端一致
        System.out.println("pic = " + pic);
        String filename = pic.getOriginalFilename();
        System.out.println("原始文件名"+filename);
       String sub = filename.substring(filename.lastIndexOf("."));
       filename = UUID.randomUUID()+sub;
        System.out.println("新的文件名"+filename);
        //保存文件的路径
        String dirPath = "E:/files";
        File dirFile = new File(dirPath);
        if(!dirFile.exists()){
            dirFile.mkdirs();
        }
      String   filePath = dirPath+"/"+filename;
        pic.transferTo(new File(filePath));

        return "/"+filename;

    }

    @RequestMapping("/remove")
    public void remove(String url){
        //删除浏览器磁盘里的文件
     new File("e:/files"+url).delete();
    }

}
